const express = require('express');
const db = require('../utils/db.js')
const router = express.Router()
router.get('/register', (req, res) => {
  const searchSql = `SELECT * FROM user WHERE account = '${req.query.account}';`
  const inserSql = `INSERT INTO user VALUES(null,'${req.query.account}','${req.query.password}'); `
  db.query(searchSql, (err, data) => {
    if (err == null) {
      if (data.length > 0) {
        res.send({
          msg: '此用户已存在'
        })
      } else {
        db.query(inserSql, (err2, data2) => {
          if (err == null) {
            res.send({
              msg: '注册成功'
            })
          } else {
            res.send({
              msg: err2
            })
          }
        })
      }
    } else {
      res.send({
        msg: err
      })
    }
  })
})
router.post('/login', (req, res) => {
  const sql = `SELECT * FROM user WHERE account = '${req.body.account}' and password = '${req.body.password}';`
  db.query(sql, (err, data) => {
    if (err) {
      res.send({
        msg: err
      })
    } else {
      if (data.length > 0) {
        res.send({
          msg: '登录成功'
        })
      } else {
        res.send({
          msg: '帐号或密码错误'
        })
      }
    }
  })
})

module.exports = router;
